<?

//Data loading
//$title='KAS - BON';
$title2='BON SEMENTARA';
//if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
//else $perush = 'PT. MASAJI PRAYASA CARGO';

$perush = 'PT. AMPEL JAYA';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport("P","mm","Letter");
$pdf->AddPage();
$Result = GridList();
$pdf->GridTable($Result);
$pdf->Output();


function GridList () {
            $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
                    //$SQL = "SELECT * FROM ais_advance WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";

                    /*$SQL = "SELECT * , b.curr as job_costing_curr FROM ais_advance a
                            JOIN tbljoborder_costing b ON a.jo_costing_id = b.jo_costing_id
                            JOIN tblactivity_nr c ON b.activities_id = c.act_nr_id
                            JOIN tbljoborder d ON b.jo_id = d.jo_id
                            JOIN tblproject e ON d.project_id = e.project_id
                            JOIN tblprojectassignment f ON e.project_id = f.project_id
                            JOIN tbldebtur g ON f.debtur = g.CUCODE
                            WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";*/
                            //echo"$SQL"; exit;

                    $SQL = "SELECT * , a.curr as job_costing_oth_curr FROM ais_advance a
                            WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";

                    return $DBConnection->dbc->get_results($SQL,ARRAY_A);
}

function ReadHeader($nobkt) {
  global $DBConnection;
  $SQL = "SELECT *,DATE_FORMAT(tglbkt,'%d %M, %Y') as spell_date  FROM ais_advance WHERE nobkt='$nobkt' LIMIT 1";
  return $DBConnection->dbc->get_row($SQL);
}


class PDFReport extends PDF {

    function Header() {

        global $title,$title2,$perush,$page,$DEFINE_FONT, $DBConnection;
        if ($DEFINE_FONT==0) {
            $this->AddFont('Arial','','arialn.php');
            $DEFINE_FONT=1;
        }
                $Field = ReadHeader(GetParam("nobkt",""));
                $this->spell_date = strtoupper($Field->spell_date);
                list($this->year,$this->month,$this->day) = explode("-",$Field->tglbkt);
                 
				//set left margin
		        $this->SetLeftMargin(15); 
				 
                //$this->SetLeftMargin(5);
                $this->SetFont('Arial','',10);
                $this->Cell(50,1,"",0,0,'L',0);
                $this->Ln();
                $this->Cell(50,1,"",0,0,'L',0);
                $this->Ln();
                $this->Cell(50,5,$perush,0,0,'C',0);
                $this->Cell(130,5,GetParam("nobkt",""),0,0,'R',0);
                $this->Ln();
                $this->Cell(50,5,"JAKARTA",0,0,'C',0);
                $this->Ln();
                $this->SetFont('Arial','',16);
                //$this->Cell(200,7,$title,0,1,'C',0);
                $this->Cell(180,7,$title2,0,1,'C',0);
                $this->SetFont('Arial','',10);
                $this->SetLeftMargin(5);
                $this->Ln();
                $this->SetLeftMargin(5);
                //$this->Line(5,30,210,30);
                //$this->Ln();

        //Header
        //Column widths
            $SQLCurr = "SELECT DISTINCT curr as job_costing_curr FROM ais_advance a
                        WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";

            $Curr = $DBConnection->dbc->get_var($SQLCurr);

            $w=array(10,130,40);
			
			//set left margin
		    $this->SetLeftMargin(15);
			
            $this->SetFont('Arial','',8);
            //$this->Cell(205,7,"NO BUKTI: ".GetParam("nobkt",""),1,0,'L',0);
            //$this->Ln();
            $this->Cell(40,7,"Tanggal Pembayaran Oleh Kasir ",'LBT',0,'L',0);
            $this->Cell(140,7,": ".$this->day."/".$this->month."/".$this->year,'RBT',0,'L',0);
            $this->Ln();
            $this->Cell(40,7,"Diselesaikan paling lambat tanggal ",'LBT',0,'L',0);
            $this->Cell(140,7,": ",'RBT',0,'L',0);
            $this->Ln();
            $this->Cell($w[0],7,"NO.",1,0,'C',0);
            //$this->Cell($w[1],7,"URAIAN",1,0,'C',0);
            $this->Cell($w[1],7,"ACTIVITIES NAME",1,0,'C',0);
            $this->Cell($w[2],7,"JUMLAH ($Curr)",1,0,'C',0);
            $this->Ln();
    }

    function GridTable($MyResult) {
        //Column widths
        global $DBConnection,$DBGeneral;
        $w=array(10,130,40);
		
		$urut = 1;
		
		//set left margin
		$this->SetLeftMargin(15);
		
        //Data
        if ($MyResult)
		{
		
			foreach($MyResult as $row)
			{
			
				$Total = $Total + $row["jumlah"];
				$this->Cell($w[0],7,$urut.".",'LR',0,'R');
				$this->Cell($w[1],7,$row["uraian"],'LR',0,'L');            
				$this->Cell($w[2],7,FormatNumber($row["jumlah"]),'LR',0,'R');
				
				$this->SetFont('Arial','',8);
				$this->Ln(5);
				
				$urut++;

			}
			
	    }
            $this->Cell($w[0],7,"",'LR',0,'R');
            $this->Cell($w[1],7,"",'LR',0,'L');
        $this->Cell($w[2],7,"",'LR',0,'R');
            $this->Ln();
                $cc = new intToRupiah;
                $GetCurrency = $DBConnection->dbc->get_var("SELECT DISTINCT tunam FROM tblcurrency, ais_advance WHERE ais_advance.nobkt='".GetParam("nobkt","")."' AND tblcurrency.tusymbol=ais_advance.curr AND ktrasl='".GetOfficeID()."'");
                //$DBConnection->dbc->debug();
                $cc->setTail("");
                $cc->setNumber($Total);
                $SpellNumber = ucfirst(strtoupper(trim($cc->currency)));

            $this->Cell($w[0],7,"",'LR',0,'R');
            $this->Cell($w[1],7,"TERBILANG:     ".$SpellNumber,'LR',0,'L');
        $this->Cell($w[2],7,"",'LR',0,'R');
            $this->Ln();
            $this->Cell($w[0]+$w[1],7,"TOTAL:",1,0,'R',0);
            $this->Cell($w[2],7,FormatNumber($Total,2),1,0,'R',0);
            $this->Ln();
            $this->Ln();

               
                /*
                $GetPerson = $DBConnection->dbc->get_row("SELECT * FROM tblprofile", ARRAY_A);
               
				$totalAll_menyetujui = $Total;
				$totalAll_tgjwb = $Total;
				  
                if($totalAll_menyetujui <= 1000000)
				{
					$ttd_menyetujui = $GetPerson["ttd_setuju_oth_kur"];
				}
				
				else 
				{
				    $ttd_menyetujui = $GetPerson["ttd_setuju_oth_lbh"];
				}
				
				if($totalAll_tgjwb <= 1000000)
				{
					$ttd_tgjwb = $GetPerson["ttd_tgjwb_oth_kur"];
				}
				
				else 
				{
				    $ttd_tgjwb = $GetPerson["ttd_tgjwb_oth_lbh"];
				}
				*/
				
				//CURRENCY
				$SQLCurr = "SELECT DISTINCT curr as job_costing_curr FROM ais_advance a
							WHERE nobkt='".GetParam("nobkt","")."' ORDER BY urut";

				$Curr = $DBConnection->dbc->get_var($SQLCurr);
				
				//Currency Rate
				$SQLGetKurs = "SELECT * FROM ais_kurssi WHERE curr = 'USD' ORDER BY tglbkt DESC";
				$ResultGetKurs  = $DBConnection->dbc->get_row($SQLGetKurs,ARRAY_A);   

				$kursUSD = $ResultGetKurs['rate'];
				
				
				if($Curr == "USD")
				{
					$totalAll_menyetujui = $Total * $kursUSD;
				    $totalAll_tgjwb = $Total * $kursUSD;
				}
				
				else
				{
					$totalAll_menyetujui = $Total;
				    $totalAll_tgjwb = $Total;
				}
				
								
				$ttd_menyetujui = "";
				$ttd_tgjwb = "";
				
				
				
				$SQLGetSetujuBayar = "SELECT * FROM tblreportsign as a 
				                      LEFT JOIN ".$DBGeneral.".tblstaff as b on a.staff_id = b.staff_id   
				WHERE a.assignment = 'setuju_bayar' AND a.report_kind = 'bs_other' ORDER BY a.report_sign_id ASC ";
				
				$GetPersonSetujuBayar = $DBConnection->dbc->get_results($SQLGetSetujuBayar, ARRAY_A);
                
				
				if(!empty($GetPersonSetujuBayar))
				{
				
					foreach($GetPersonSetujuBayar as $idx => $val)
					{
					   $range_left = $val['range_left'];
					   $range_right = $val['range_right'];
					   
					   if(($totalAll_menyetujui >= $range_left) && ($totalAll_menyetujui <= $range_right))
					   {
							$ttd_menyetujui = $val["nama"];
					   }	
					
					}
				
			    }
				
								
				
				$SQLGetTanggungJawab = "SELECT * FROM tblreportsign as a 
				                      LEFT JOIN ".$DBGeneral.".tblstaff as b on a.staff_id = b.staff_id  
				WHERE a.assignment = 'penanggung_jawab' AND a.report_kind = 'bs_jo' ORDER BY a.report_sign_id ASC ";
				
				$GetPersonTanggungJawab = $DBConnection->dbc->get_results($SQLGetTanggungJawab, ARRAY_A);
                
                
				if(!empty($GetPersonTanggungJawab))
				{
				
					foreach($GetPersonTanggungJawab as $idx => $val)
					{
					   $range_left = $val['range_left'];
					   $range_right = $val['range_right'];
					   
					   if(($totalAll_tgjwb >= $range_left) && ($totalAll_tgjwb <= $range_right))
					   {
							$ttd_tgjwb = $val["nama"];
					   }	
					
					}
				
			    }
                
                

                $this->Cell(60,5,"Setuju Bayar",1,0,'C',0);
                $this->Cell(60,5,"Penanggung Jawab",1,0,'C',0);
                $this->Cell(60,5,"Penerima",1,0,'C',0);
                $this->Ln();
                $this->Cell(10,5,"Tanggal",1,0,'C',0);
                $this->Cell(50,5,"",1,0,'C',0);
                $this->Cell(10,5,"Tanggal",1,0,'C',0);
                $this->Cell(50,5,"",1,0,'C',0);
                $this->Cell(10,5,"Tanggal",1,0,'C',0);
                $this->Cell(50,5,"",1,0,'C',0);
                $this->Ln();
                $this->Cell(10,5,"Nama",1,0,'C',0);
                $this->Cell(50,5,$ttd_menyetujui,1,0,'C',0);
                $this->Cell(10,5,"Nama",1,0,'C',0);
                $this->Cell(50,5,$ttd_tgjwb,1,0,'C',0);
                $this->Cell(10,5,"Nama",1,0,'C',0);
                $this->Cell(50,5,$row["debnam"],1,0,'C',0);
                $this->Ln();
                $this->Cell(10,8,"Ttd",1,0,'C',0);
                $this->Cell(50,8,"",1,0,'C',0);
                $this->Cell(10,8,"Ttd",1,0,'C',0);
                $this->Cell(50,8,"",1,0,'C',0);
                $this->Cell(10,8,"Ttd",1,0,'C',0);
                $this->Cell(50,8,"",1,0,'C',0);

            $this->Ln();$this->Ln();$this->Ln();

                //$this->Cell(68,7,"( ".$GetPerson["ttdbs1"]." )",1,0,'C',0);
                //$this->Cell(69,7,"( ".$GetPerson["ttdbs2"]." )",1,0,'C',0);
                //$this->Cell(68,7,"( ".$row["debnam"]." )",1,0,'C',0);



        //Closure line
    }

    function Footer() {
        //Closure line
        //$this->Cell(205,0,'','T');
    }

}


?>
